<template>
	<view>
		<view v-if="pageLoad">
			<form @submit="submit">
				<view class="input-flex">
					<view class="input-flex-label">昵称</view>
					<input class="input-flex-text" name="nickname" v-model="user.nickname">
					<view class="input-flex-label">
						<picker @change="bindPickerChangeGender" :value="genderIndex" :range="genderList"
							range-key="title">
							<view class="uni-input">性别：{{genderList[genderIndex].title}}</view>
						</picker>
					</view>
					<view class="input-flex-label">
						<picker @change="bindPickerChangeType" :value="typeIndex" :range="list" range-key="title">
							<view class="uni-input">类型：{{typeTitle == ''? '':list[typeIndex].title}}</view>
						</picker>
					</view>
				</view>
				<button form-type="submit" class="btn-row-submit">提交</button>
			</form>
		</view>
	</view>
</template>

<script>
	import list from '../../common/new.js'
	export default {
		data: function() {
			return {
				list: list,
				pageLoad: false,
				user: {},
				unLogin: true,
				ssuserid: uni.getStorageSync('userInfo')._id || this.$store.getters.userInfo._id,
				typeTitle: '',
				typeIndex: 0,
				genderList: [{
					title: '未知',
					value: 0
				}, {
					title: '男',
					value: 1
				}, {
					title: '女',
					value: 2
				}],
				genderIndex: 0,
			}
		},
		onLoad: function(option) {
			this.getPage();
		},

		methods: {
			bindPickerChangeType(e) {
				this.typeTitle = this.list[e.target.value]
				this.typeIndex = e.target.value
			},
			bindPickerChangeGender(e) {
				this.genderIndex = e.target.value
			},
			getPage: function() {
				var that = this;
				uniCloud.callFunction({
					name: "user-center",
					data: {
						"action": "get",
						id: this.ssuserid
					},
					success: function(e) {
						var res = e.result;
						if (res.error) {
							return false;
						}
						console.log(res)
						that.pageLoad = true;
						that.user = res.data.user;
						that.genderIndex = that.user.gender;
						that.typeIndex = that.user.userType
						that.typeTitle = that.list[that.typeIndex]
						console.log(JSON.stringify(that.user));
					}
				})
			},
			submit: function(e) {
				var that = this;
				console.log(that.typeIndex)
				console.log(that.user.nickname)
				console.log(that.genderIndex)
				uniCloud.callFunction({
					name: "user-center",
					data: {
						"action": "undata",
						id: this.ssuserid,
						params: {
							userType: that.typeIndex,
							nickname: that.user.nickname,
							gender: that.genderIndex,
						}
					},
					success: function(e) {
						uni.showToast({
							title: e.result.message || e.result.msg,
							icon: "none"
						})
						if (e.result) {
							setTimeout(() => {
								uni.navigateBack()
							}, 800)
						}
					},
					fail(err) {
						uni.showToast({
							title: err.result.msg,
							icon: "none"
						})
					}
				})
			}
		},
	}
</script>
<style>

</style>
